package core2.ch04database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JtdsConnMssql {
	public static void main(String[] args) {
			
		try {
			Class.forName("net.sourceforge.jtds.jdbc.Driver"); //������������
			Connection con = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/wfsys;user=sa;password=sa"); //������ݿ�����
			//����Statement����
					
			Statement stmt = con.createStatement(
			ResultSet.TYPE_SCROLL_INSENSITIVE,
			ResultSet.CONCUR_READ_ONLY);
			
			System.out.println("<SCRIPT type=text/javascript>");
			System.out
			.println("var tree = new MzTreeView('tree');tree.setIconPath('../images/menu/');tree.icons[\"book\"]  = \"book.gif\";tree.iconsExpand[\"book\"] = \"bookopen.gif\";");


			ResultSet rs =stmt.executeQuery("select icon from mtree order by parentId,nodeId ");
			while(rs.next()){
//				���ͼƬ�� icons ������
				String iconName =rs.getString("icon");
				String iconKey=iconName.substring(0,iconName.indexOf("."));
				
				System.out.println("tree.icons[\""+iconKey+"\"] = \""+iconName+"\";");
			}
			rs.close();
			
			rs = stmt
			.executeQuery("select * from mtree order by parentId,nodeId"); //����ResultSet(���)���󣬲�ִ��SQL���

			
			while (rs.next()) {

				StringBuilder value = new StringBuilder("");

				if (rs.getString("text") != null) {
			value.append("text:" + rs.getString("text") + ";");
				}

				if (rs.getString("icon") != null) {
				
				String iconName =rs.getString("icon");
				iconName=iconName.substring(0,iconName.indexOf("."));
			
				//���� ��ǰ�Ľڵ� ͼ��
				value.append("icon:" + iconName + ";");


				}

				if (rs.getString("url") != null) {
			value.append("url:" + rs.getString("url") + ";");
				}

				value.deleteCharAt(value.lastIndexOf(";"));

				System.out.println("tree.nodes[\"" + rs.getString("parentId")
				+ "_" + rs.getString("nodeId") + "\"] = \"" + value
				+ "\";");
			}
			System.out
			.println("tree.setTarget('frameShow');document.write(tree.toString());</SCRIPT>");
			
			rs.close();
			stmt.close();

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException sqle) {
			sqle.printStackTrace();
		}

	}

}
